//
//  RootViewController.m
//  UI09_UITableView
//
//  Created by yutao on 15/10/21.
//  Copyright © 2015年 yutao. All rights reserved.
//

#import "RootViewController.h"
#import "Singleton.h"
#import "Student.h"
@interface RootViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.  

    [Singleton shareSingleton];

    self.view.backgroundColor = [UIColor lightGrayColor];
    UITableView *myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 667-64) style:UITableViewStylePlain];
    myTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:myTableView];
    [myTableView release];

    myTableView.delegate = self;//基本配置代理
    myTableView.dataSource = self;//数据源代理



}

//设置section个数 区的个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [Singleton shareSingleton].studentDic.allKeys.count;
}

//设置每个section的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{   //根据section区的下标取出key
    NSString *key = [[Singleton shareSingleton].studentDic.allKeys objectAtIndex:section];
    //根据key取出对应的数组
    NSMutableArray *array = [[Singleton shareSingleton].studentDic objectForKey:key];
    //当前section的行数由数组个数决定
        return array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   
    //1.先从重用池里取
    //queue:队列
    //reusabele:重用 reuse
    //identifier:标识符
    //标识符是区别不同cell的唯一标记

    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    //if(!cell)
    if (cell == nil) {
        //如果cell为空,需要创建cell
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];

    }
    

    NSString *key = [[Singleton shareSingleton].studentDic.allKeys objectAtIndex:indexPath.section];
    NSMutableArray *array = [[Singleton shareSingleton].studentDic objectForKey:key];
    Student *stu = [array objectAtIndex:indexPath.row];
    cell.textLabel.text = stu.name;



    return cell;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
